package com.pcbly.web.servlet;

import java.awt.Color;
import java.awt.Font;
import java.util.Random;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;

public class AuthImg extends ListImageCaptchaEngine {
	/**
	 *
	 */
	private static final long serialVersionUID = -7099312208878086233L;

	@Override
	protected void buildInitialFactories() {
		// TODO Auto-generated method stub
		WordGenerator wgen = new RandomWordGenerator(
				"abcdefghijklmnopqrstuvwxyz123456789");
		RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(
				new int[] { 200, 250 }, new int[] { 200, 250 },
				new int[] { 200, 250 });
		TextPaster textPaster = new RandomTextPaster(new Integer(4),
				new Integer(4), cgen, true);
		BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(
				new Integer(60), new Integer(40));
		Font[] fontsList = new Font[] { new Font("Arial", Font.PLAIN, 18),
				new Font("Tahoma", Font.PLAIN, 18), new Font("Verdana", Font.PLAIN, 18),  new Font("Times New Roman", Font.PLAIN, 18),};
		FontGenerator fontGenerator = new RandomFontGenerator(new Integer(15),
				new Integer(30), fontsList);
		WordToImage wordToImage = new ComposedWordToImage(fontGenerator,
				backgroundGenerator, textPaster);
		addFactory(new GimpyFactory(wgen, wordToImage));
	}
	public Color getRandColor(int fc, int bc) {
		// 给定范围获得随机颜色
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}
}
